<?php

/**
 * This is the model class for table "{{academy}}".
 *
 * The followings are the available columns in table '{{academy}}':
 * @property integer $id
 * @property integer $profile_id
 * @property string $best_subject
 * @property string $major
 * @property string $certificate
 * @property string $language
 * @property string $create_date
 * @property string $created_by
 * @property string $update_date
 * @property string $updated_by
 *
 * The followings are the available model relations:
 * @property Profile $profile
 * @property StudyingProcess[] $studyingProcesses
 */
class Academy extends CActiveRecord
{
	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return Academy the static model class
	 */
	public static function model($className=__CLASS__)
	{
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName()
	{
		return '{{academy}}';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules()
	{
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('profile_id', 'required' , 'message' => t ( 'MSG_REQUIRED', 'messages' )),
			array('profile_id', 'numerical', 'integerOnly'=>true, 'message' => t ( 'MSG_WRONG_NUMBER', 'messages' )),
			array('best_subject, major, certificate', 'length', 'max'=>255, 'tooLong' => t ( 'MSG_STRING_TOO_LONG', 'messages' )),
			array('language, created_by, updated_by', 'length', 'max'=>50, 'tooLong' => t ( 'MSG_STRING_TOO_LONG', 'messages' )),
			array('create_date, update_date', 'safe'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('id, profile_id, best_subject, major, certificate, language, create_date, created_by, update_date, updated_by', 'safe', 'on'=>'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations()
	{
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'profile' => array(self::BELONGS_TO, 'Profile', 'profile_id'),
			'studyingProcesses' => array(self::HAS_MANY, 'StudyingProcess', 'academy_id'),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels()
	{
		return array(
			'id' => t('LBL_ID', 'academy'),
			'profile_id' => t('LBL_ID', 'academy'),
			'best_subject' => t('LBL_BEST_SUBJECT', 'academy'),
			'major' => t('LBL_MAJOR', 'academy'),
			'certificate' => t('LBL_CERTIFICATE', 'academy'),
			'language' => t('LBL_LANGUAGE', 'academy'),
			'LBL_CREATE_DATE' => t('LBL_ID', 'academy'),
			'LBL_CREATED_BY' => t('LBL_ID', 'academy'),
			'LBL_UPDATE_DATE' => t('LBL_ID', 'academy'),
			'LBL_UPDATED_BY' => t('LBL_ID', 'academy'),
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search()
	{
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria=new CDbCriteria;

		$criteria->compare('id',$this->id);
		$criteria->compare('profile_id',$this->profile_id);
		$criteria->compare('best_subject',$this->best_subject,true);
		$criteria->compare('major',$this->major,true);
		$criteria->compare('certificate',$this->certificate,true);
		$criteria->compare('language',$this->language,true);
		$criteria->compare('create_date',$this->create_date,true);
		$criteria->compare('created_by',$this->created_by,true);
		$criteria->compare('update_date',$this->update_date,true);
		$criteria->compare('updated_by',$this->updated_by,true);

		return new CActiveDataProvider($this, array(
			'criteria'=>$criteria,
		));
	}
        
         public function beforeSave() {
            if (parent::beforeSave()) {

                $current_date = date(LGConstant::DATE_FORMAT_FOR_DATABASE);

                //Get user is logging
                $logedInUser = User::model()->findByPk(Yii::app()->user->id);	   

                $this->update_date = $current_date;

                if (isset($logedInUser)) {
                    $this->updated_by = $logedInUser->username;
                }	                            

                $this->setIsNewRecord(false);

                if (isset($this->yearOfBirth) && isset($this->monthOfBirth) && isset($this->dayOfBirth) &&
                $this->yearOfBirth != "" && $this->monthOfBirth != "" && $this->dayOfBirth != "" ){
                    $this->birth_date = $this->yearOfBirth . "-" . $this->monthOfBirth . "-" . $this->dayOfBirth;            
                } 

                return true;
            } else {
                return false;
            }
        }
}